﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using ITAP.Core.Calculations;
using ITAP.ContourBuilder.Components;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandNavigation : Command
    {
        public CommandNavigation(ContourControl control)
            : base(control)
        {
        }

        Point prevMousePoint;
        bool canSelectByPoint = false;
        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);
            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == MouseButtons.Left)
            {
                prevMousePoint = state.MouseState.State.Location;
                ModelPoint point = this.ScreenPointToModelPoint(prevMousePoint);
                IEnumerable<IContourComponent> allComponents = this.control.ContourContainer.GetAllInnerComponents();
                foreach (IContourComponent component in allComponents)
                    if (component.CanSelectByPoint(point))
                    {
                        canSelectByPoint = true;
                        return;
                    }

                canSelectByPoint = false;
            }
            else if (state.MouseState.MouseMode == MouseMode.Move && state.MouseState.State.Button == MouseButtons.Left)
            {
                if (canSelectByPoint)
                    return;

                ModelPoint prevPoint = this.ScreenPointToModelPoint(prevMousePoint);
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                ModelVector vector = new ModelVector(prevPoint, point);
                this.control.CameraCenter = ModelCalculations.GetOffsetPoint(this.control.CameraCenter, vector.Rotate(new Angle(Math.PI)));
                this.control.Invalidate();
                prevMousePoint = state.MouseState.State.Location;
            }
            else if (state.MouseState.MouseMode == MouseMode.Wheel)
            {
                this.control.UseAutoScale = false;

                if (state.MouseState.State.Delta > 0)
                    this.control.ZoomOut(this.control.CameraCenter, (double)Math.Abs(state.MouseState.State.Delta) / 100.0);
                else
                    this.control.ZoomIn(this.control.CameraCenter, (double)Math.Abs(state.MouseState.State.Delta) / 100.0);
                this.control.Invalidate();

            }
        }

        public override string ToString()
        {
            return "Навигация";
        }

        public override bool CanUndo
        {
            get
            {
                return false;
            }
        }
    }
}
